package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.Service.ActorService;
import com.example.bysj.domain.User;
import com.example.bysj.domain.authority.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
//声明数据库访问类
@Repository("UserDao")
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    ActorService actorService;
    public User getByUserName(String name){
        String sql = "SELECT * FROM User where username = ?";

        List<User> users = jdbcTemplate.query(sql, new Object[]{name}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));

                return user;
            }
        });
        if (!users.isEmpty()){
            return users.get(0);
        } else
            return null;
    }
    //增加
    public boolean add(User user) {
        String sql = "INSERT INTO User (username,password,actor_id)" +
                "VALUES(?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, user.getUsername(),
                user.getPassword(),user.getActor().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //查询全部
    public List<User> findAll(){
        String sql = "SELECT * FROM User ";
        List<User> Users = jdbcTemplate.query(sql, new UserRowMapper());
        return Users;
    }
    //登录方法，根据user对象返回actor对象
    public Actor login(User user){
        String sql = "SELECT * FROM user WHERE username =? AND password=?";
        List<Actor> actors = jdbcTemplate.query(sql,
                new Object[]{user.getUsername(),user.getPassword()}, new RowMapper(){
                    //定义匿名类
                    @Override
                    public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                        int actor_id =resultSet.getInt("actor_id");
                        Actor actor =actorService.find(actor_id);
                        return actor;
                    }
                });

        if(!actors.isEmpty()){
            return actors.get(0);
        } else
            return null;
    }
    //通过定义内部类来返回查询结果
    public class UserRowMapper implements RowMapper<User>{
        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException {

            int id=resultSet.getInt("id");
            String username=resultSet.getString("username");
            String password=resultSet.getString("password");

            Actor actor =actorService.find(resultSet.getInt("actor_id"));
            //时间戳对象
            Timestamp timestamp = resultSet.getTimestamp("lastLoginTime");
            LocalDateTime lastLoginTime = null;
            if (timestamp != null) {
                lastLoginTime = timestamp.toLocalDateTime();
            }
            User user = new User();
            user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            user.setActor(actor);
            user.setLoginTime(lastLoginTime);
            return user;
        }
    }
}
